package Student;

import java.util.ArrayList;
import java.util.*;

public class StudentManager {
    private ArrayList<Student> students = new ArrayList<>();

    // 存储考勤记录
    private Map<String, List<AttendanceRecord>> attendanceRecords = new HashMap<>();
    // 存储答题记录
    private Map<String, List<AnswerRecord>> answerRecords = new HashMap<>();

    public void addStudent(Student student) {
        students.add(student);
    }

    public void removeStudent(String id) {
        students.removeIf(student -> student.getId().equals(id));
    }

    public Student getStudent(String id) {
        return students.stream()
                .filter(student -> student.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    public ArrayList<Student> getAllStudents() {
        return new ArrayList<>(students);
    }

    public ArrayList<Student> getStudentsByClass(String className) {
        ArrayList<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getClassName().equals(className)) {
                result.add(student);
            }
        }
        return result;
    }

    public ArrayList<Student> getStudentsByGroup(String groupName) {
        ArrayList<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getGroupName().equals(groupName)) {
                result.add(student);
            }
        }
        return result;
    }

    // 根据ID获取学生
    public Student getStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    // 记录考勤
    public void recordAttendance(String studentName, String status, String date) {
        Student student = getStudentByName(studentName);
        if (student == null) return;

        if (!attendanceRecords.containsKey(studentName)) {
            attendanceRecords.put(studentName, new ArrayList<>());
        }
        attendanceRecords.get(studentName).add(new AttendanceRecord(student.getId(), date, status));
    }

    // 获取考勤记录
    public List<AttendanceRecord> getAttendanceRecords(String studentName) {
        return attendanceRecords.getOrDefault(studentName, new ArrayList<>());
    }

    // 记录答题情况
    public void recordAnswer(String studentName, String questionId, boolean correct, String date) {
        Student student = getStudentByName(studentName);
        if (student == null) return;

        if (!answerRecords.containsKey(studentName)) {
            answerRecords.put(studentName, new ArrayList<>());
        }
        answerRecords.get(studentName).add(new AnswerRecord(student.getId(), questionId, correct, date));
    }

    // 获取答题记录
    public List<AnswerRecord> getAnswerRecords(String studentName) {
        return answerRecords.getOrDefault(studentName, new ArrayList<>());
    }

    // 根据姓名获取学生
    private Student getStudentByName(String name) {
        for (Student student : students) {
            if (student.getName().equals(name)) {
                return student;
            }
        }
        return null;
    }

    // 考勤记录内部类
    public static class AttendanceRecord {
        public String studentId;
        public String date;
        public String status;

        public AttendanceRecord(String studentId, String date, String status) {
            this.studentId = studentId;
            this.date = date;
            this.status = status;
        }
    }

    // 答题记录内部类
    public static class AnswerRecord {
        public String studentId;
        public String questionId;
        public boolean correct;
        public String date;

        public AnswerRecord(String studentId, String questionId, boolean correct, String date) {
            this.studentId = studentId;
            this.questionId = questionId;
            this.correct = correct;
            this.date = date;
        }
    }

}
